package com.reefe.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.reefe.entity.ZTreeNode;
import com.reefe.exception.AppException;
import com.reefe.mapper.TbItemCatMapper;
import com.reefe.pojo.TbItemCat;
import com.reefe.pojo.TbItemCatExample;
import com.reefe.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xiao on 2017-12-23.
 */
@Component
@Service(version = "1.0.0",timeout = 30000)
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Override
    public List<ZTreeNode> getCatList(long parentId) {
        // 1、根据parentId查询节点列表
        TbItemCatExample example = new TbItemCatExample();
        //设置查询条件
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> list = itemCatMapper.selectByExample(example);
        // 2、转换成ZTreeNode列表。
        List<ZTreeNode> resultList = new ArrayList<>();
        for (TbItemCat tbItemCat : list) {
            ZTreeNode zTreeNode = new ZTreeNode();
            zTreeNode.setId(Math.toIntExact(tbItemCat.getId()));
            zTreeNode.setStatus(tbItemCat.getStatus());
            zTreeNode.setSortOrder(tbItemCat.getSortOrder());
            zTreeNode.setName(tbItemCat.getName());
            zTreeNode.setpId(Math.toIntExact(tbItemCat.getParentId()));
            zTreeNode.setIsParent(tbItemCat.getIsParent());
            zTreeNode.setRemark(tbItemCat.getRemark());
            //添加到列表
            resultList.add(zTreeNode);
        }
        // 3、返回。
        return resultList;
    }

    @Override
    public TbItemCat getItemCatById(Long id) {

        TbItemCat tbItemCat=itemCatMapper.selectByPrimaryKey(id);
        if(tbItemCat==null){
            throw new AppException("通过id获取商品分类失败");
        }
        return tbItemCat;
    }

    @Override
    public int updateItemCat(TbItemCat tbItemCat) {
        TbItemCat old=getItemCatById(tbItemCat.getId());
        tbItemCat.setCreated(old.getCreated());
        tbItemCat.setUpdated(new Date());

        if(itemCatMapper.updateByPrimaryKey(tbItemCat)!=1){
            throw new AppException("更新商品分类失败");
        }
        return 1;
    }

    @Override
    public int addItemCat(TbItemCat tbItemCat) {

        tbItemCat.setCreated(new Date());
        tbItemCat.setUpdated(new Date());

        if(itemCatMapper.insert(tbItemCat)!=1){
            throw new AppException("添加商品分类失败");
        }
        return 1;
    }
    @Override
    public void deleteItemCat(Long id) {

        deleteZTree(id);
    }

    @Override
    public void deleteZTree(Long id) {

        //查询该节点所有子节点
        List<ZTreeNode> node = getCatList(Math.toIntExact(id));
        if(node.size()>0){
            //如果有子节点，遍历子节点
            for(int i=0;i<node.size();i++){
                deleteItemCat((long) node.get(i).getId());
            }
        }
        //没有子节点直接删除
        if(itemCatMapper.deleteByPrimaryKey(id)!=1){
            throw new AppException("删除商品分类失败");
        }
    }

}
